<template>
	<div class="system-allocation-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="20">
					<el-col :xs="24" :sm="12" :md="10" :lg="12" :xl="12" class="mb20">
						<el-form-item>
							<el-select v-model="state.ruleForm.select_id" placeholder="请选择部门" size="default">
									<el-option v-for="item in state.deptData" :key="item.value" :label="item.label"
										:value="item.value" />
								</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="10" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="抽取">
								<el-select v-model="state.ruleForm.answer_id" placeholder="请选择部门" size="default">
									<el-option v-for="item in state.deptData" :key="item.value" :label="item.label"
										:value="item.value" />
								</el-select>
							</el-form-item>
						</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useDepartmentApi } from '/@/api/department';
import { useAllocationtApi } from '/@/api/allocation';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		select_id: '', // 出题部门
		answer_id: '', // 被抽取部门
		status: true, // 分配状态
		createTime: new Date().toLocaleString(),//创建时间
	},
	deptData: [] as any[], // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: any) => {
	state.dialog.type = type
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改分配';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增分配';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	userDialogFormRef.value.resetFields();
		// });
	}
	state.dialog.isShowDialog = true;
	getMenuData();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (state.dialog.type === 'add') {
		useAllocationtApi().AddSelect_Rule(state.ruleForm).
			then((res) => {
				console.log(`res`, res);
				ElMessage.success('添加成功');
				closeDialog();
				emit('refresh');
			}).catch((err) => {
				console.log(`err`, err);
				ElMessage.success('添加失败');
			})
	}else if (state.dialog.type === 'edit') {
		console.log(`修改`);
	}
};
// 初始化部门数据
const getMenuData = () => {
	useDepartmentApi().GetDepartment({}).
		then((res) => {
			let deptData: any[] = []
			console.log(`部门数据`, res);
			res.data.forEach((element: { id: any; dept_name: any; }) => {
				deptData.push({
					value: element.id,
					label: element.dept_name,
				});
			});
			state.deptData = deptData
		})
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
